<?php
require_once('Kbs/Controller/Action.php');

class Admin_PostController extends Kbs_Controller_Action
{
    public function init()
    {
        /* Should be open in production !!!!!!!!
         * Should be open in production !!!!!!!!
         * Should be open in production !!!!!!!!
        
        $frontendOptions = array(
           'lifetime' => 7200, 
           'debug_header' => true,
           'regexps' => array(
                            '^/admin$' => array('cache' => false),
                            '^/admin/post/' => array('cache' => false),
                            '^/admin/post/index' => array('cache' => false),
                            '^/admin/post/edit' => array('cache' => false),
                       ),
           'default_options' => array(
                'cache_with_get_variables' => true,
                'cache_with_post_variables' => true,
                'make_id_with_cookie_variables' => true,
                'cache_with_session_variables' => true,
                'cache_with_files_variables' => true,
                'cache_with_cookie_variables' => true,
            )
        );

        $backendOptions = array(
            'cache_dir' => 'G:/WWW/kbs/repository/cache/' 
        );

        $cache = Zend_Cache::factory('Page', 
                                     'File', 
                                     $frontendOptions, 
                                     $backendOptions);
        $cache->start();*/
    }

    public function indexAction()
    {
        $this->_forward('list');
    }

    public function listAction()
    {
        $postObj = new Kbs_Model_Business_Post();
        $this->view->posts = $postObj->fetchAll();
    }

    public function gridAction()
    {
        $postObj = new Kbs_Model_Business_Post();
        $posts = $postObj->fetchAll();

        if ($posts) {
            $paginator = Zend_Paginator::factory($posts);
            $page = $this->_getParam('page');
            $paginator->setCurrentPageNumber($page);
            $paginator->setItemCountPerPage(5);

            if (count($paginator)) {
                $this->view->posts = $paginator;
            }
        }
    }

    // to edit the post
    public function editAction()
    {
        $categoryObj = new Kbs_Model_Business_Post_Category();
        $this->view->categoryOptions = $categoryObj->getCategoryOptions();

        $localeObj = new Kbs_Model_Business_Locale();
        $this->view->locales = $localeObj->fetchAll();

        if ($this->getRequest()->isGet()) {
            if ($this->_hasParam('id')) {
                $id = $this->_getParam('id');

                $postObj = new Kbs_Model_Business_Post();
                $post = $postObj->find($id);

                if (!empty($post)) {
                    $this->view->postId           = $post->postId;
                    $this->view->postCategoryId   = $post->postCategoryId;
                    $this->view->postUrlTitle     = $post->postUrlTitle;
                    $this->view->postCreatedAt    = $post->postCreatedAt;
                    $this->view->postUpdatedAt    = $post->postUpdatedAt;
                    $this->view->postAdjusted     = $post->postAdjusted;
                    $this->view->postPublished    = $post->postPublished;

                    $this->view->details = $post->fetchDetails();
                }
            }
        }
    }

    // to save the post
    public function saveAction()
    {
        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();

            $postObj = new Kbs_Model_Business_Post();
            $psotId = $postObj->savePostAndDetails($data);

            $this->_redirect('/admin/post');

        } else {
            $this->_redirect('/admin');
        }
    }

    // to delete the post
    public function deleteAction()
    {
        if ($this->getRequest()->isGet()) {
            $id = $this->_getParam('id');

            $postObj = new Kbs_Model_Business_Post();
            $postObj->deletePostAndDetails($id);

            $this->_redirect('/admin/post');
        } else {
            $this->_redirect('/admin');
        }
    }

}